<?php
namespace app\common\model;

use think\Model;
use think\Db;
class QuestionDb extends Model
{
    // 指定表名,不含前缀
    protected $name = 'question_db';
/*     // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    //自动完成
    protected $auto = ['create_time','update_time'];

    protected function setCreateTimeAttr($value)
    {
        return date('Y-m-d H:i:s',time());
    } */

    /**
     * 根据id获取规则id集合
     * @param $id
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getRuleidsByid($id){
        if (!$id) return false;
        $ruleid = $this->field('d_ruleid')->where('id = '.$id)->find();
        return $ruleid;
    }
	
	 public function tablename()
	{
		return 'ex_'.$this->name;		
	}
    /**
     * 根据id 获取试题
     * @param $id
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public function getDdataByid($id){
        if (!$id) return false;
        $ruleid = $this->field('d_data')->where('id = '.$id)->find();
        return $ruleid;
    }
    /**
     * 根据参数获取列表
     * @param array $arg ['p_subject'=>2,'id'=>1....]
     * @param $field 'id,p_subject...'
     */
    public function getListByArg(array $arg=null,$field='*'){
        if (!is_array($arg))return false;
        $arg = $this->where($arg)->field($field)->select();
        return $arg;
    }
	
	
	
	/* 新设计 */
	
	/* 分题库根据知识点ID 获取试题 */
	/* 以下两个函数今后可与Question model合并 */
	
	public function getQuestionIds($qbankid,$questype=0,$knowsid=0,$num=1)
	{
		
		if($qbankid) $sql_where['q_qbankid'] = $qbankid;
		if($questype) $sql_where['q_typeid'] = $questype;
		if($knowsid) $sql_where['q_knowsid'] = $knowsid;
		
		$res = Db::name($this->name)->field('id')->where($sql_where)->select();

		foreach($res as $val){
			$result[] = $val['id'];	
		}		
		return $result;
	}
	
	public function getQuestionsById($ids){
		if(empty($ids)) return false;
		return Db::name($this->name)->where('id',['in',$ids])->order('id asc')->select();
	}
	
	/* 更新或者新增函数 */
	public function batDataSave($data){
		$ret = $this->allowField(true)->isUpdate(false)->saveAll($data) ;		
		return $ret;
	}
	
}
